/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain.controller.rms;

import java.util.Enumeration;
import java.util.Hashtable;
import domain.model.rms.MObjectRMS;

/**
 *
 * @author Victorino
 */
public class MObjectController {

    private static Hashtable cache = new Hashtable(0);

    public static Enumeration listMObject(int mObjectLoader) {

        Integer index = new Integer(mObjectLoader);
        Hashtable mobjects = (Hashtable) cache.get(index);

        if (mobjects != null) {
            int dbSize = RecordStorePool.getInstance().getNumRecords(index.intValue());
            int msize = mobjects.size();
            if (dbSize <= msize && dbSize != 0) {
                return mobjects.elements();
            }
        }

        Enumeration en = RecordStorePool.getInstance().getAllRecords(index.intValue()).elements();
        while (en.hasMoreElements()) {
            MObjectRMS mo = (MObjectRMS) MObjectLoader.newClass(index.intValue());
            mo.unSerialize((byte[]) en.nextElement());
            addObjectInCache(mo);
        }
        Hashtable ht = (Hashtable) cache.get(index);
        return ht == null ? new Hashtable(0).elements() : ht.elements();
    }

    public static MObjectRMS createMObject(int mObjectType) {

        return createMObject(mObjectType, new Integer(-1));

    }

    public static MObjectRMS createMObject(int mObjectType, int id) {

        return createMObject(mObjectType, new Integer(id));

    }

    public static void clearCache() {

        Enumeration en = cache.keys();
        while (en.hasMoreElements()) {
            Integer mKey = (Integer) en.nextElement();
            Hashtable mObjects = (Hashtable) cache.get(mKey);
            Enumeration en1 = mObjects.keys();
            while (en1.hasMoreElements()) {
                Integer key = (Integer) en1.nextElement();
                MObjectRMS obj = (MObjectRMS) mObjects.remove(key);
                obj = null;
            }
            mObjects = null;
            cache.remove(mKey);
        }
        cache = null;
        cache = new Hashtable(0);
        System.gc();
    }

    public static void removeObjectInCache(MObjectRMS mo) {

        Hashtable objects = (Hashtable) cache.get(new Integer(mo.getLoaderType()));
        if (objects == null) {
            objects = new Hashtable();
        }

        objects.remove(mo.getId());

        cache.put(new Integer(mo.getLoaderType()), objects);
        System.gc();
    }

    public static void addObjectInCache(MObjectRMS mo) {
        if (Runtime.getRuntime().freeMemory() < 15000) {
            clearCache();
            System.gc();
        }

        if (mo == null) {
            return;
        }
        Integer index = new Integer(mo.getLoaderType());
        Hashtable objects = (Hashtable) cache.get(index);
        if (objects == null) {
            objects = new Hashtable();
        }
        objects.remove(mo.getId());

        objects.put(mo.getId(), mo);

        cache.put(index, objects);
    }

    public static MObjectRMS createMObject(int mObjectType, Integer id) {

        MObjectRMS retVal = null;

        if (id.intValue() == -1) {// retorna novo obj

            return (MObjectRMS) MObjectLoader.newClass(mObjectType);

        }

        Hashtable mobjects = (Hashtable) cache.get(new Integer(mObjectType));
        if (mobjects != null) {
            // retorna se encontrado no cache
            retVal = (MObjectRMS) mobjects.get(id);

        }
        if (retVal == null) {

            retVal = MObjectLoader.newClass(mObjectType);
            retVal.load(id);

            addObjectInCache(retVal);
            System.gc();
        }
        return retVal;
    }
}
